#include "CommonContainer.h"

CommonContainer::CommonContainer(const QString &title, bool hasEditBtn, int bgType, QWidget *parent)
    : QWidget(parent)
    , m_isEditMode(false), m_bgType(bgType)
{
    setupUI();
    m_titleLabel->setText(title);
    m_editButton->setVisible(hasEditBtn);
    
    connect(m_editButton, &QPushButton::clicked, this, &CommonContainer::onEditButtonClicked);
}

CommonContainer::~CommonContainer()
{
}

void CommonContainer::setupUI()
{
    m_mainLayout = new QVBoxLayout(this);
    m_mainLayout->setContentsMargins(10, 0, 10, 10);
    m_mainLayout->setSpacing(10);

    m_topWt = new QWidget(this);
    QHBoxLayout *hLayout = new QHBoxLayout(m_topWt);
    hLayout->setContentsMargins(10, 0, 10, 0);
    hLayout->setSpacing(22);
    m_titleLabel = new QLabel();
    m_titleLabel->setObjectName("commonTitleLabel");
    m_editButton = new QPushButton("编辑");
    m_editButton->setObjectName("commonEditButton");
    QVBoxLayout *vLayout = new QVBoxLayout();
    vLayout->setContentsMargins(0, 4, 0, 4);
    vLayout->addWidget(m_editButton);
    hLayout->addWidget(m_titleLabel);
    hLayout->addStretch();
    hLayout->addLayout(vLayout);

    m_mainLayout->addWidget(m_topWt);
}

void CommonContainer::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, false);  // 禁用抗锯齿以保持像素清晰
    
    QPixmap backgroundPixmap(":/img/images/Title Bar0" + QString::number(m_bgType) + ".png");
    if (!backgroundPixmap.isNull()) {
        // 使用九宫格绘制方式，保持边框线条不变形
        int borderSize = 10; // 假设边框大小为10px
        QRect targetRect = rect();
        QRect sourceRect = backgroundPixmap.rect();
        
        // 计算九宫格的各个区域
        QRect topLeft(0, 0, borderSize, borderSize);
        QRect topCenter(borderSize, 0, sourceRect.width() - 2*borderSize, borderSize);
        QRect topRight(sourceRect.width() - borderSize, 0, borderSize, borderSize);
        
        QRect centerLeft(0, borderSize, borderSize, sourceRect.height() - 2*borderSize);
        QRect center(borderSize, borderSize, sourceRect.width() - 2*borderSize, sourceRect.height() - 2*borderSize);
        QRect centerRight(sourceRect.width() - borderSize, borderSize, borderSize, sourceRect.height() - 2*borderSize);
        
        QRect bottomLeft(0, sourceRect.height() - borderSize, borderSize, borderSize);
        QRect bottomCenter(borderSize, sourceRect.height() - borderSize, sourceRect.width() - 2*borderSize, borderSize);
        QRect bottomRight(sourceRect.width() - borderSize, sourceRect.height() - borderSize, borderSize, borderSize);
        
        // 绘制九宫格
        painter.drawPixmap(targetRect.topLeft(), backgroundPixmap, topLeft);
        painter.drawPixmap(QRect(targetRect.left() + borderSize, targetRect.top(), 
                                targetRect.width() - 2*borderSize, borderSize), 
                          backgroundPixmap, topCenter);
        painter.drawPixmap(QRect(targetRect.right() - borderSize, targetRect.top(), 
                                borderSize, borderSize), 
                          backgroundPixmap, topRight);
        
        painter.drawPixmap(QRect(targetRect.left(), targetRect.top() + borderSize, 
                                borderSize, targetRect.height() - 2*borderSize), 
                          backgroundPixmap, centerLeft);
        painter.drawPixmap(QRect(targetRect.left() + borderSize, targetRect.top() + borderSize, 
                                targetRect.width() - 2*borderSize, targetRect.height() - 2*borderSize), 
                          backgroundPixmap, center);
        painter.drawPixmap(QRect(targetRect.right() - borderSize, targetRect.top() + borderSize, 
                                borderSize, targetRect.height() - 2*borderSize), 
                          backgroundPixmap, centerRight);
        
        painter.drawPixmap(QRect(targetRect.left(), targetRect.bottom() - borderSize, 
                                borderSize, borderSize), 
                          backgroundPixmap, bottomLeft);
        painter.drawPixmap(QRect(targetRect.left() + borderSize, targetRect.bottom() - borderSize, 
                                targetRect.width() - 2*borderSize, borderSize), 
                          backgroundPixmap, bottomCenter);
        painter.drawPixmap(QRect(targetRect.right() - borderSize, targetRect.bottom() - borderSize, 
                                borderSize, borderSize), 
                          backgroundPixmap, bottomRight);
    }
    
    QWidget::paintEvent(event);
}

void CommonContainer::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    
    int newHeight = height() / ((m_bgType == 2) ? 12 : 6);
    if (newHeight > 0) {
        m_topWt->setFixedHeight(newHeight);
    }
}

void CommonContainer::setContentWidget(QWidget *contentWt)
{
    m_mainLayout->addWidget(contentWt);
}

void CommonContainer::setContentLayout(QLayout *contentLayout)
{
    m_mainLayout->addLayout(contentLayout);
}

void CommonContainer::setEditMode(bool enabled)
{
    if (m_isEditMode == enabled) return;
    
    m_isEditMode = enabled;
    
    // 更新按钮文本
    if (m_editButton) {
        m_editButton->setText(enabled ? "完成" : "编辑");
    }
    
    emit editModeChanged(enabled);
}

bool CommonContainer::isEditMode() const
{
    return m_isEditMode;
}

void CommonContainer::onEditButtonClicked()
{
    // 切换编辑模式
    setEditMode(!m_isEditMode);
    
    // 发送编辑按钮点击信号
    emit editButtonClicked();
}